package com.erp.erp_project.controller;

import com.erp.erp_project.core.annotation.Permission;
import com.erp.erp_project.model.Customer;
import com.erp.erp_project.model.Order;
import com.erp.erp_project.model.Product;
import com.erp.erp_project.model.Result;
import com.erp.erp_project.service.ICustomService;
import com.erp.erp_project.service.IOrderService;
import com.erp.erp_project.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author the bright star
 */
@Controller
@AllArgsConstructor
@RequestMapping("product")
public class ProductController {
    private IOrderService orderService;

    private ICustomService customService;

    private IProductService productService;


    @RequestMapping("/topicture")
    public String toPicture(Model model,@Param("id") Integer id){
        model.addAttribute("id",id);
        return "product/picture";
    }
    @RequestMapping("/tonote")
    public String toNote(Model model,@Param("id") Integer id){
        model.addAttribute("id",id);
        return "product/requirement";
    }

    @RequestMapping("/tolist")
    public String toList(Model model){
        return "product/list";
    }

    @RequestMapping("/toedit")
    public String toEdit(Model model,@Param("id") Integer id){
        model.addAttribute("id",id);
        return "product/edit";
    }
    @RequestMapping("/toadd")
    public String toAdd(Model model){
        return "product/add";
    }


    @RequestMapping("/list")
    @ResponseBody
    public Result<PageInfo<Product>> list(@Param("page") Integer page,@Param("limit") Integer limit){
        PageHelper.startPage(page,limit);
        List<Product> products=productService.getAll();
        PageInfo<Product> pageInfo=new PageInfo<>(products);
        Result<PageInfo<Product>> result=new Result<>(0,"查询成功");
        result.setData(pageInfo);
        return result;
    }

    @ResponseBody
    @RequestMapping("/getpicture")
    public List<String> getPicture(@Param("id") Integer id){
        Product product=productService.getProductById(id);
        String image=product.getImage();
        if (image!=null){
            String[] imgs=image.split(",");
            List<String> list=new ArrayList<>();
            for (int i=0;i<imgs.length;i++){
                list.add(imgs[i]);
            }
            return list;
        }
        return new ArrayList<>();
    }

    @RequestMapping("/deletepic")
    @ResponseBody
    public Result<String> deletePic(@Param("data") String data,@Param("id") Integer id,@Param("dels") String dels){
        String[] delArray=dels.split(",");
        for (String del: delArray) {
            if (del.equals("")){
                continue;
            }
            File file=new File("D:/Picture/pic/"+del.substring(5));
            if (file.exists()){
                file.delete();
            }
        }
        productService.updatePic(id,data);
        return new Result<>(200,"删除成功");
    }

    @RequestMapping("/uploadpic")
    @ResponseBody
    @Permission("产品修改")
    public Result<List<String>> uploadPic(MultipartFile[] files, @Param("id") Integer id){
        System.out.println(files);
        if (files.length==0){
            return new Result<>(100,"没有文件");
        }
        List<String> names=productService.uploadPic(files,id);
        Result<List<String>> result=new Result<>(200,"上传成功");
        result.setData(names);
        return result;
    }

    @RequestMapping("/add")
    @ResponseBody
    @Permission("产品添加")
    public Result<String> add(@Param("product") Product product){
        productService.addProduct(product);
        return new Result<>(200,"添加成功");
    }

    @RequestMapping("/edit")
    @ResponseBody
    @Permission("产品修改")
    public Result<String> edit(Product product){
        productService.editProduct(product);
        return new Result<>(200,"编辑成功");
    }

    @RequestMapping("/delete")
    @ResponseBody
    @Permission("产品删除")
    public Result<String> delete(String ids){
        String[] integers=ids.split(",");
        List<Integer> list=new ArrayList<>();
        for (String s: integers) {
            list.add(Integer.parseInt(s));
        }
        productService.deleteProduct(list);
        return new Result<>(20,"删除成功");
    }

    @RequestMapping("/search")
    @ResponseBody
    public Result<PageInfo<Product>> search(Integer page, Integer limit, Integer type, String condition){
        PageHelper.startPage(page,limit);
        List<Product> products=productService.searchProduct(type,condition);
        PageInfo<Product> info=new PageInfo<>(products);
        Result<PageInfo<Product>> result=new Result<>(0,"查询成功");
        result.setData(info);
        return result;
    }

    @ResponseBody
    @RequestMapping("/loadedit")
    @Permission("产品修改")
    public Product loadEdit(@Param("id") Integer id){
        Product product=productService.getProductById(id);
        return product;
    }
    @RequestMapping("/note")
    @ResponseBody
    @Permission("产品修改")
    public Result<String> setNote(@Param("note") String note,Integer id){
        productService.setNote(note,id);
        return new Result<>(200,"操作成功");
    }
}
